home *** CD-ROM | disk | FTP | other *** search
- Path: newshost.lanl.gov!tanmoy
- From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
- Newsgroups: comp.lang.c
- Subject: Re: Confusion as to the proper use of MODULAS
- Date: 15 Feb 1996 00:26:28 GMT
- Organization: Los Alamos National Laboratory
- Message-ID: <TANMOY.96Feb14172628@qcd.lanl.gov>
- References: <4fr8be$ass@news.iconn.net> <31224679.6193@born.com>
- <4fthsu$1kl@ixnews7.ix.netcom.com>
- NNTP-Posting-Host: qcd.lanl.gov
- Mime-Version: 1.0
- Content-Type: text
- In-reply-to: wzjn@ix.netcom.com's message of 14 Feb 1996 20:49:34 GMT
-
- In article <4fthsu$1kl@ixnews7.ix.netcom.com> wzjn@ix.netcom.com (KPN
- ) writes:
- <snip>
- Confusion as to the proper use of MODULAS
-
- That is a funny word. I know of modulus ... guess you must be talking
- about the same concept :-) More commonly, the result of % is called
- the remainder, and is the basis of a branch of arithmetic called
- modular arithmetic.
-
- <snip>
- Modulas operator simply shows: *** AFTER *** you divide, what is the
- remainder?
-
- Well it couldn't be the remainder, before you divide, could it be?
- After all before you can think of something remaining, you have to do
- something, right? So, yes, the result of x % y is what remains of x
- when you have taken out as many y's as you could out of it. The
- precise meaning is in terms of division: it says, that you divide x by
- y to get an integral value, multiply the result back by y and subtract
- from x. Thus x % y = x - (x/y)*y
-
- EXAMPLE of this:
- 9 % 7 = ?
- 9 divided by 7 yields a remainder of 2
-
- Another way of looking at it is that you can take one 7 out of 9, and
- after taking it away, you have 2 left over. You cannot take out any
- more 7's, so the result is 7.
-
- Of course 9/7 is 1 by the same count. So, 9 % 7 = 9-(9/7)*7 = 2.
-
- 9 % 7 = 2
-
- 1) MODULAS means divide a number and give me the left over number. OK -
- not bad.
-
- Correct. This is always the meaning. x % y = x - (x/y)*y, always (or
- rather when x/y is makes sense. Aside for experts: the standard seems
- to require no overflow on the rhs ... I had not expected this: maybe I
- am misreading the standard in my hurry to finish this post.)
-
- Next rule:
- If the number to be divided is smaller than the number to divide by,
- the result will always be the number being divided
-
- This is a subcase of the previous rule. If you cannot take any y's out
- of x, then what remains is the whole of x, right?
-
- EXAMPLE of this:
-
- 9 % 12 = ?
- can not divide 9 by a larger number
- 9 % 12 = 9
- Now I have rule #2
-
- Yes. But see, you cannot take 12 out of 9: so, what is left over is
- 9. Doing it the other way, 9/12=0 (i.e. no 12's can be taken out of
- 9), so 9 % 12 = 9-(9/12)*12 = 9
-
- 2) If the number to be divided is smaller than the nuber to be used as
- a divisor, the result is the original number. OK - again, not bad.
-
- Do you see that this rule is not really needed.
-
- Here, begin my troubles: using MODULAS in an IF statement.
-
- I made up a test to see if a number I inputted was a 7. Sample code:
-
- if (number % 7)
- printf("Not a 7\n");
- else
- printf("First integer was a 7\n");
-
- So, if the number WAS a 7, the ELSE would take over. OK. But, when I
- enter in a ZERO, it still tells me that the number was a seven?
-
- Well, what does your second rule say? Isn't ZERO less that 7? In that
- case shouldn't the answer of number % 7 be number itself? And the
- number is ZERO.
-
- Can you see that number % 7 is the same for number being 0, 7, 14, 21
- etc.
-
- Am I going in the right direction? Can someone tell me what IÆm doing
-
- I do not know. I have no idea where you are going ...
-
- incorrectly, or where IÆm straying? WhatÆs the rule here for using MOD
- in an IF statement?
-
- There is no IF statement, there is an if statement though. C is case
- sensitive, so you have to use the correct case. The only rule is that
- if the expression controlling the if is zero (of any kind, or a null
- pointer), the controlled statement is executed. If the value is
- non-zero (or a non-null pointer), the else branch, if any, is executed.
-
- Thank you for your help,
- kevin
-
- There is only one issue which you have to be careful about. If the
- numbers being divided are not positive, x % y = x - (x/y)*y is still
- correct, but the meaning of x/y may vary from one machine to another,
- and from one compiler to another. Thus (-3) / 2 can be either -1 or
- -2, but if (-3)/2 = -1, then (-3) % 2 is -1; whereas if (-3)/2 is -2,
- (-3) % 2 is +1.
-
- Cheers
- Tanmoy
- --
- tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
- Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
- Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
- <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
- internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
- fax: 1 (505) 665 3003 voice: 1 (505) 665 4733 [ Home: 1 (505) 662 5596 ]
-